/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogCrearHoraExtra.java
 *
 * Created on 14/04/2011, 11:25:25 PM
 */

package frontera.interfaz.dialogo;

import control.SIRHApp;
import entidad.Empleado;
import frontera.interfaz.panel.PanelHorasExtraEmpleado;
import entidad.HoraExtra;
import java.awt.Color;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;

/**
 *
 * @author Silex RPR
 */
public class DialogCrearHoraExtra extends javax.swing.JDialog {

    /** Creates new form DialogCrearHoraExtra */
    public DialogCrearHoraExtra(java.awt.Frame parent, boolean modal, int indice, 
            HoraExtra horaExtra, PanelHorasExtraEmpleado pHEE, JButton boton, Empleado empleado) {
        super(parent, modal);
        initComponents();
        
        setLocationRelativeTo(null);
        inicializarVariables(indice, horaExtra, pHEE, boton, empleado);
    }

    private void inicializarVariables(int indice, HoraExtra horaExtra, PanelHorasExtraEmpleado pHEE, JButton boton,
            Empleado empleadoHoras){
        
        DialogCrearHoraExtra.panelHorasExtraEmpleado = pHEE;
        DialogCrearHoraExtra.horaExtra = horaExtra;
        DialogCrearHoraExtra.indice = indice;
        this.tipoHora = "Diurna";
        this.cantidadHoras = 1;
        labelFecha.setText(DialogCrearHoraExtra.horaExtra.getFecha().get(Calendar.DATE) +
                           "/" +
                           ( DialogCrearHoraExtra.horaExtra.getFecha().get(Calendar.MONTH) + 1 ) +
                           "/" +
                           DialogCrearHoraExtra.horaExtra.getFecha().get(Calendar.YEAR));
        
        DialogCrearHoraExtra.boton = boton;
        empleado = empleadoHoras;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tipoHorasCB = new javax.swing.JComboBox();
        botonCancelar = new javax.swing.JButton();
        labelFecha = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        aceptarB = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        cantidadHorasCB = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Agregando Horas Extras");

        tipoHorasCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Diurna", "Nocturna", "Festiva", "FestivaNocturna" }));
        tipoHorasCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tipoHorasCBActionPerformed(evt);
            }
        });

        botonCancelar.setText("Cancelar");
        botonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCancelarActionPerformed(evt);
            }
        });

        labelFecha.setFont(new java.awt.Font("Dialog", 0, 18));

        jLabel3.setFont(new java.awt.Font("Dialog", 1, 18));
        jLabel3.setText("Fecha:");

        aceptarB.setText("Aceptar");
        aceptarB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aceptarBActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Cantidad De Horas Extras");

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/frontera/recursos/hExtra.png"))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Tipo De Horas Extras");

        cantidadHorasCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7" }));
        cantidadHorasCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cantidadHorasCBActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(151, 151, 151)
                        .addComponent(aceptarB)
                        .addGap(18, 18, 18)
                        .addComponent(botonCancelar))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(cantidadHorasCB, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(tipoHorasCB, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(labelFecha, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)))))
                .addContainerGap(16, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(labelFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(tipoHorasCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(cantidadHorasCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(aceptarB)
                            .addComponent(botonCancelar)))
                    .addComponent(jLabel5))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tipoHorasCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tipoHorasCBActionPerformed
        JComboBox cb = (JComboBox)evt.getSource();
        tipoHora = (String)cb.getSelectedItem();
}//GEN-LAST:event_tipoHorasCBActionPerformed

    private void aceptarBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aceptarBActionPerformed

        try{
            if ( panelHorasExtraEmpleado.cantidadHorasExtras(panelHorasExtraEmpleado.getNuevasHorasExtras()) + cantidadHoras  <= 24 ){
                horaExtra.setCantidadHoras(cantidadHoras);
            }

            else{
                throw new NumberFormatException();
            }


        } catch(Exception e){
            JOptionPane.showMessageDialog(null, "Revise que no se haya excedido el limite de horas por quincena." ,"Error",JOptionPane.ERROR_MESSAGE);
            return;
        }

        HoraExtra.tipoHora tipoHoraM = HoraExtra.tipoHora.valueOf(this.tipoHora);
        horaExtra.setTipoHoraExtra(tipoHoraM);
        horaExtra.setEmpAsociado(SIRHApp.getInstance().getDAOEmpleado().getEmpleadoID(empleado.getID()));
        
        panelHorasExtraEmpleado.getNuevasHorasExtras().add(horaExtra);
        
        boton.setBackground(Color.green);

        this.setVisible(false);
        this.dispose();

    }//GEN-LAST:event_aceptarBActionPerformed

    private void botonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCancelarActionPerformed
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_botonCancelarActionPerformed

    private void cantidadHorasCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cantidadHorasCBActionPerformed
        JComboBox cb = (JComboBox)evt.getSource();
        cantidadHoras  = Integer.parseInt( (String)cb.getSelectedItem());
    }//GEN-LAST:event_cantidadHorasCBActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DialogCrearHoraExtra dialog = new DialogCrearHoraExtra(new javax.swing.JFrame(), true, indice, horaExtra,
                        panelHorasExtraEmpleado, boton, empleado);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton aceptarB;
    private javax.swing.JButton botonCancelar;
    private javax.swing.JComboBox cantidadHorasCB;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel labelFecha;
    private javax.swing.JComboBox tipoHorasCB;
    // End of variables declaration//GEN-END:variables
    private static HoraExtra horaExtra;
    private static int indice;
    private String tipoHora;
    private static PanelHorasExtraEmpleado panelHorasExtraEmpleado;
    private static JButton boton;
    private static Empleado empleado;
    private int cantidadHoras;
}
